﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;

namespace LogicUniversity.clerks
{
    public partial class MaintainCatalogueInfo : System.Web.UI.Page
    {
        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CatalogueMaintain cm = new CatalogueMaintain();
                List<Catelogue> c = cm.getCatalogue();
                DataBind();
                fillDataGrid();
            }
        }
        public void fillDataGrid()
        {
            CatalogueMaintain cm = new CatalogueMaintain();

            List<Catelogue> ca = cm.getCatalogue();
            GridView1.DataSource = ca;
            GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            CatalogueMaintain cm = new CatalogueMaintain();
            List<Catelogue> ca = cm.getAllbyName(Convert.ToString(TextBox2.Text));
         
            GridView1.DataSource = ca;
            GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {            
            Response.Redirect("AddCatalogue.aspx");    
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

            AddCatalogue add = new AddCatalogue();           
            GridViewRow row = GridView1.SelectedRow;
            
            string item_Code = row.Cells[1].Text.ToString();
            string reorder_Level = row.Cells[2].Text.ToString();
            string reorder_Qty = row.Cells[3].Text.ToString();
           //string measurement = row.Cells[4].Text.ToString();
            string item_Descrip = row.Cells[6].Text.ToString();
            string bin = row.Cells[7].Text.ToString();
          // string category = row.Cells[8].Text.ToString();
            Session["Item_Code"] = item_Code;
            Session["Reorder_Level"] = reorder_Level;
            Session["Reorder_Qty"] = reorder_Qty;
          // Session["Measurement"] = measurement; 
            Session["Item_Description"] = item_Descrip;
            Session["Bin"] = bin;
         //  Session["Category"] = category;

             Response.Redirect("UpdateCatalogue.aspx");
        }
        
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
           
                String itemCode = GridView1.DataKeys[e.RowIndex].Value.ToString();
                
                CatalogueMaintain caMaintain = new CatalogueMaintain();
                
                caMaintain.deleteCatalogue(itemCode);
                
                fillDataGrid();
                Alert.Show("Delete Catalogue susseccfully");
            }
        public static class Alert
        {
            public static void Show(string message)
            {
                // Cleans the message to allow single quotation marks
                string cleanMessage = message.Replace("'", "\\'");
                string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

                // Gets the executing web page
                Page page = HttpContext.Current.CurrentHandler as Page;

                // Checks if the handler is a Page and that the script isn't allready on the Page
              //  if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
               // {
                    page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
               // }
            }
        }   
        }    
       
    }
